package com.blue.controller.workflow;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.blue.common.annotation.NotNull;
import com.blue.common.annotation.ReplaceParam;
import com.blue.common.constant.CommonConstant;
import com.blue.vo.workflow.DefinitionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.utils.QueryWrapperUtils;
import org.springframework.web.bind.annotation.*;
import com.blue.common.utils.ConvertUtils;
import com.blue.po.workflow.DefinitionPO;
import com.blue.dto.workflow.DefinitionDTO;
import com.blue.service.workflow.IDefinitionService;
import com.blue.common.entity.ResponseResult;

import java.util.List;


/**
 * 流程定义表
 *
 * @author nanjie.chen
 * @email 15696471158@163.com
 * @date 2025-03-07
 */
@RestController
@RequestMapping("/workflow/definition")
@Tag(name = "流程定义表")
public class DefinitionController {
    @Autowired
    private IDefinitionService definitionService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    @ReplaceParam("definitionNm")
    public ResponseResult<?> list(DefinitionDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        DefinitionPO definitionPO = ConvertUtils.convert(DefinitionPO.class, requestDTO);
        MPJQueryWrapper<DefinitionPO> queryWrapper = QueryWrapperUtils.initQueryWrapper(definitionPO);
        Page<DefinitionPO> page = new Page<>(pageNum, pageSize);
        Page<DefinitionPO> pageResult = definitionService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseResult<?> add(@RequestBody DefinitionDTO requestDTO) {
        definitionService.add(requestDTO);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改")
    public ResponseResult<?> edit(@RequestBody DefinitionDTO requestDTO) {
        definitionService.edit(requestDTO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    public ResponseResult<?> delete(@RequestBody List<String> ids) {
        definitionService.update(new LambdaUpdateWrapper<DefinitionPO>()
                .set(DefinitionPO::getIsFlag, CommonConstant.NO)
                .in(DefinitionPO::getId, ids));
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    @NotNull(value = "id", message = "流程定义ID不能为空！")
    public ResponseResult<?> queryById(@RequestBody DefinitionDTO requestDTO) {
        DefinitionVO definitionVO = definitionService.queryById(requestDTO.getId());
        return ResponseResult.okResult(definitionVO);
    }

    @PostMapping("/changeStatus")
    @Operation(summary = "修改状态")
    @NotNull(value = "id", message = "流程定义ID不能为空！")
    @NotNull(value = "status", message = "流程定义状态不能为空！")
    public ResponseResult<?> changeStatus(@RequestBody DefinitionDTO requestDTO) {
        DefinitionPO definitionPO = ConvertUtils.convert(DefinitionPO.class, requestDTO);
        definitionService.updateById(definitionPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/enable")
    @Operation(summary = "启用流程")
    @NotNull(value = "id", message = "流程定义ID不能为空！")
    public ResponseResult<?> enable(@RequestBody DefinitionDTO requestDTO) {
        definitionService.enable(requestDTO);
        return ResponseResult.okResult();
    }
}
